package cz.data.domain.quality.controller;

import cz.data.common.base.SimpleDataResponse;
import cz.data.common.base.TableDataResponse;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cz.data.common.base.BaseController;
import cz.data.common.core.JsonPage;
import cz.data.domain.quality.mapstruct.RuleItemMapper;
import cz.data.domain.quality.model.entity.RuleItemEntity;
import cz.data.domain.quality.model.query.RuleItemQuery;
import cz.data.domain.quality.model.vo.RuleItemVo;
import cz.data.domain.quality.service.RuleItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@Api(tags = {"规则核查项"})
@RestController
@RequestMapping("/ruleItems")
public class RuleItemController extends BaseController {

    @Autowired
    private RuleItemService ruleItemService;

    @Autowired
    private RuleItemMapper ruleItemMapper;

    @ApiOperation(value = "规则核查项-详情")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @GetMapping("/{id}")
    public SimpleDataResponse<RuleItemVo> getRuleItemById(@PathVariable String id) {
        RuleItemEntity ruleItemEntity = ruleItemService.getRuleItemById(id);
        return new SimpleDataResponse<>(ruleItemMapper.toVO(ruleItemEntity));
    }

    @ApiOperation(value = "规则核查项-获取列表")
    @GetMapping("/list")
    public TableDataResponse<RuleItemEntity> getRuleTypeList(RuleItemQuery ruleItemQuery) {
        QueryWrapper<RuleItemEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(ruleItemQuery.getRuleTypeId()), "rule_type_id", ruleItemQuery.getRuleTypeId());
        List<RuleItemEntity> list = ruleItemService.list(queryWrapper);
        return new TableDataResponse<>(list);
    }

    @ApiOperation(value = "规则核查项-分页查询")
    @ApiImplicitParam(name = "ruleItemQuery", value = "查询实体ruleItemQuery", required = true, dataTypeClass = RuleItemQuery.class)
    @GetMapping("/page")
    public SimpleDataResponse<JsonPage<RuleItemVo>> getRuleItemPage(RuleItemQuery ruleItemQuery) {
        QueryWrapper<RuleItemEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(ruleItemQuery.getRuleTypeId()), "rule_type_id", ruleItemQuery.getRuleTypeId());
        IPage<RuleItemEntity> page = ruleItemService.page(new Page<>(ruleItemQuery.getPageNum(), ruleItemQuery.getPageSize()), queryWrapper);
        List<RuleItemVo> collect = page.getRecords().stream().map(ruleItemMapper::toVO).collect(Collectors.toList());
        JsonPage<RuleItemVo> jsonPage = new JsonPage<>(page.getCurrent(), page.getSize(), page.getTotal(), collect);
        return new SimpleDataResponse<>(jsonPage);
    }
}
